#!/usr/bin/python3


from optparse import OptionParser
import sys
import os

sys.path.append("/usr/lib/linx-sec-center")
import ct_core

def create_parser():
    USAGE_MES="%prog [-a|--append]\
            \n  Default: when you input no argument, you could input mac\
            \n          subject labels in stdin, and when you finished, Ctrl+D\
            \n          will be back and write the new label to system if the\
            \n          The inputs correct\
            \n Example:\
            \n secadmin~$ %prog \
            \n 1000 2 3 4\
            \n 1001 4 5 6\
            \n EOF          "
    parse = OptionParser(usage=USAGE_MES,version="%prog 1.0")
    parse.add_option("-a","--append",action="store_true", dest="append", help="add a mac subject label to system")    
    parse.add_option("-c","--clear",action="store_true", dest="clear", help="clear up subject label of the system")    
    return parse

def set_sub_label(flg=''):
    if flg != '':
        ct_core.get_mac_sub_label()
    print("Please input the new subject labels:")
    line = sys.stdin.readlines()
    for l in line:
        ls = l.split()
        if len(ls) != 4:
            print("You input subject label %s error!" %ls)
            return
        uid,f1,f2,f3 = ls
        if not ct_core.set_mac_sub_label(uid, f1, f2, f3):
            return
    ct_core.write_mac_sub_file()

def clear_sub_label():
    ct_core.d_sub_label.clear()
    ct_core.write_mac_sub_file()
    print("Clear mac subject label done!")

def main():
    parser = create_parser()
    args,flg = parser.parse_args()

    if args.append:
        set_sub_label('a')
    elif args.clear:
        clear_sub_label()
    elif not flg:
        set_sub_label()
    else:
        parser.error("Argument error, please specify a correct argument")

main()
